package com.yilin.tms.core.commons.zysdk;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SignStringUtil {
    public static final String CHARSET_NAME_UTF8 = "UTF-8";
    public static final char[] digital = "0123456789ABCDEF".toCharArray();
    public static final String DEFAULT_DATA_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    public static String format(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        return format.format(date);
    }

    public static String encodeHexStr(byte[] bytes) {
        if (bytes == null) {
            return null;
        } else {
            char[] result = new char[bytes.length * 2];

            for(int i = 0; i < bytes.length; ++i) {
                result[i * 2] = digital[(bytes[i] & 240) >> 4];
                result[i * 2 + 1] = digital[bytes[i] & 15];
            }

            return new String(result);
        }
    }

    public static byte[] decodeHexStr(String str) {
        if (str == null) {
            return null;
        } else {
            char[] charArray = str.toCharArray();
            if (charArray.length % 2 != 0) {
                throw new RuntimeException("hex str length must can mod 2, str:" + str);
            } else {
                byte[] bytes = new byte[charArray.length / 2];

                for(int i = 0; i < charArray.length; ++i) {
                    char c = charArray[i];
                    int b;
                    if (c >= '0' && c <= '9') {
                        b = c - 48 << 4;
                    } else {
                        if (c < 'A' || c > 'F') {
                            throw new RuntimeException("unsport hex str:" + str);
                        }

                        b = c - 65 + 10 << 4;
                    }

                    ++i;
                    c = charArray[i];
                    if (c >= '0' && c <= '9') {
                        b |= c - 48;
                    } else {
                        if (c < 'A' || c > 'F') {
                            throw new RuntimeException("unsport hex str:" + str);
                        }

                        b |= c - 65 + 10;
                    }

                    bytes[i / 2] = (byte)b;
                }

                return bytes;
            }
        }
    }

    public static String toString(byte[] bytes) {
        if (bytes == null) {
            return null;
        } else {
            try {
                return new String(bytes, "UTF-8");
            } catch (UnsupportedEncodingException var2) {
                throw new RuntimeException(var2.getMessage(), var2);
            }
        }
    }

    public static String toString(byte[] bytes, String charset) {
        if (bytes == null) {
            return null;
        } else {
            try {
                return new String(bytes, charset);
            } catch (UnsupportedEncodingException var3) {
                throw new RuntimeException(var3.getMessage(), var3);
            }
        }
    }

    public static byte[] toBytes(String str) {
        if (str == null) {
            return null;
        } else {
            try {
                return str.getBytes("UTF-8");
            } catch (UnsupportedEncodingException var2) {
                throw new RuntimeException(var2.getMessage(), var2);
            }
        }
    }

    private SignStringUtil() {
    }
}
